package com.suntine.familytree.test.goodluck;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @corporation 
 * @author suntine
 * @creation 2011-5-18 上午10:46:04
 * @description 
 */
public class BT2 {
	
	class SingleNumber{
		public String number;
		public int targetTimes;
		public int latestAppear;
		public int rank ;
		
		public SingleNumber(String number, int targetTimes){
			this.number = number;
			this.targetTimes = targetTimes;
		}
	}
	
	class Answer{
		private int length = 5;
		private String answer = "";
		private int sn = 0;
		
		public Answer() {

		}
		
		public Answer(String answer, int sn) {
			this.answer = answer;
			this.sn = sn;
		}
		public boolean containTheNumber(BT2.SingleNumber snObj){
			String an[] = this.answer.split(" ");
			for(String aItem : an){
				if(aItem.equals(snObj.number)){
					return true;
				}
			}
			return false;
		}
		
	}
	
	class Option{
		private int flag = 0;
		private String option = "";
		private int length = 0;
		private List<Integer> targetResult = new ArrayList<Integer>();
		private int targetTimes = 0;
		private boolean isContinued = false;
		private int sumRank;
		
		public Option() {

		}
		
		public Option(String option, int length, int flag) {
			this.option = option;
			this.length = length;
			this.flag = flag;
			String o[] = this.option.split(" ");
			
			boolean breakFlag = false;
			for(int i=1;i<o.length;i++){
				int a = Integer.parseInt(o[i-1]);
				int b = Integer.parseInt(o[i]);
				if(b-a>1){
					breakFlag = true;
					break;
				}
			}
			if(breakFlag){
				isContinued = false;
			}else{
				isContinued = true;
			}
			
		}
		
		public void setSumRank(Map<String,BT2.SingleNumber> map){
			int sr = 0;
			String o[] = this.option.split(" ");
			for(int i=0;i<o.length;i++){
				sr += map.get(o[i]).rank;
			}
			this.sumRank = sr;
		}
		
		public boolean containTheResult(int result){
			for(Integer i : this.targetResult){
				if(result == i.intValue()){
					return true;
				}
			}
			return false;
		}
		
		public int getNumbersOfSame(String numbers){
			int times = 0;
			String o[] = this.option.split(" ");
			String n[] = numbers.split(" ");
			for(String ns : n){
				for(String os : o){
					if(ns.equals(os)){
						times++;
					}
				}
			}
			return times;
		}
		
		public boolean containTheNumber(BT2.SingleNumber snObj){
			String on[] = this.option.split(" ");
			for(String oItem : on){
				if(oItem.equals(snObj.number)){
					return true;
				}
			}
			return false;
		}
		
		public String getTargetResult(){
			String resultStr = "";
			for(int r : this.targetResult){
				if(r==snMax){
					resultStr += "【当前】"+r+" ";
				}else if(r==(snMax-1)){
					resultStr += "【倒一】"+r+" ";
				}else if(r==(snMax-2)){
					resultStr += "【倒二】"+r+" ";
				}else if(r==(snMax-3)){
					resultStr += "【倒三】"+r+" ";
				}else if(r==(snMax-4)){
					resultStr += "【倒四】"+r+" ";
				}else{
					resultStr += r+" ";
				}
				
			}
			return resultStr;
		}
		
		public void targetCheck(BT2.Answer a){
			int targetTimes = 0;
			int times = 0;
			String an[] = a.answer.split(" ");
			String on[] = this.option.split(" ");
			
			if(this.length>a.length){
				targetTimes = a.length;
				
				for(String aItem:an){
					for(String oItem:on){
						if(aItem.equals(oItem)){
							times++;
						}
					}
				}
			}else{
				targetTimes = this.length;
				
				for(String oItem:on){
					for(String aItem:an){
						if(oItem.equals(aItem)){
							times++;
						}
					}
				}
			}
			
			if(targetTimes == times){
				this.targetResult.add(a.sn);
				this.targetTimes++;
			}
		}
	}
	
	
	private static List<BT2.Answer> answerList ;
	private static List<BT2.Option> optionList ;
	private static String numbers[] = new String[11];
	private static String selectNumbers[] = new String[8];
	//11 选8 ，中20，备选165，几率：0.12121212121212121212121212121212
	//11 选5 ，中1，   备选462，几率：0.0021645021645021645021645021645022
	private static Map<String,BT2.Option> advice ;
	private static List<BT2.Option> adviceList ;
	private static int snMax = 0;
	private static int counter = 0;
	private static int numberRateScope = 11;//8:6 5:12
	private static Map<String,BT2.SingleNumber> numberMap ;
//	private static String TESTPRINT[] = new String[]{"0","0","0"};
	private static boolean TEST = false;
	private static String TPAN ; //test presetting answer number
	private static int TPA_SN = 64; //test prestting answer sn
	private static boolean printFlag = true;
	private static BT2.Answer latestAnswer ;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int sumAnswers = 0;
		int sumTargets = 0;
		//numberRateScope+2
		for(int t=64;t<=64;t++){
			sumAnswers++;
			answerList = new ArrayList<BT2.Answer>();
			optionList = new ArrayList<BT2.Option>();
			advice = new HashMap<String,BT2.Option>();
			adviceList = new ArrayList<BT2.Option>();
			numberMap = new HashMap<String,BT2.SingleNumber>();
			for(int y=1;y<=numbers.length;y++){
				String n = y<10?"0"+y:""+y;
				numbers[y-1]=n;
				numberMap.put(n, new BT2().new SingleNumber(n,0));
			}
			TPA_SN = t;
			TPAN = "";
			try {
				
				dataCollector();
				
				select(0,1,numbers,selectNumbers);
				
				if(analyzer()){
				
					//打印
					/*if(printFlag){
						System.out.println("\n第"+(snMax+1)+"期  共"+optionList.size()+"个备选组合，机器人挑选出"+advice.entrySet().size()+"条建议给您参考，随机显示如下：\n");
					}
					int u = 1;
					for (Iterator it = advice.entrySet().iterator(); it.hasNext();) {
					      Map.Entry e = (Map.Entry) it.next();
					      BT2.Option o = (BT2.Option)e.getValue();
					      //打印
						  if(printFlag){
						      System.out.print(u+"——"+o.flag+"  "+e.getKey()+" 命中次数："+o.targetTimes+" 期数（"+o.getTargetResult()+"）"+" 阶层和为："+o.sumRank+" ");
						  }    
					      if(!"".equals(TPAN) && o.getNumbersOfSame(TPAN)==5){
							  if(printFlag){
								  System.out.print("★★★\n");
							  }
						  }else{
							  if(printFlag){
								  System.out.print("\n");
							  }
						  }
						  
					      u++;
				     }
					*/
					
					//打印
					if(printFlag){
						System.out.println("\n\n按命中次数递减显示：\n");
					}
					int n = 1;
					int lastTargetTimes = 0;
					int starTime = 0;
					for(BT2.Option o : adviceList){
						
						if(n>1 && lastTargetTimes!=o.targetTimes){
							if(printFlag){
								//System.out.println("=====================================================================================");
							}
						}
						//打印
						if(printFlag){
							System.out.print(n+"--"+o.flag+"  "+o.option+" 命中次数："+o.targetTimes+" 期数（"+o.getTargetResult()+"）"+" 阶层和为：￥"+o.sumRank+" ");
						}
						if(!"".equals(TPAN) && o.getNumbersOfSame(TPAN)==5){
							starTime++;
							if(printFlag){
								System.out.print("★★★\n");
							}
						}else{
							if(printFlag){
								System.out.print("\n");
							}
						}
						
						lastTargetTimes = o.targetTimes;
						n++;
					}
					
					
					//打印
					//System.out.println("\n"+testPrint[0]+" "+testPrint[1]+" "+testPrint[2]);
					if(!"".equals(TPAN) && advice.size()>0){
						if(starTime>0){sumTargets++;}
						System.out.println("\n↑↑↑↑↑↑↑↑第"+(snMax+1)+"期  答案是："+TPAN+" 理论组合数："+optionList.size()+" 机器人建议数："+advice.size()+"，命中数："+starTime+" 命中率为："+(double)starTime/advice.size());
					}
					
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		
		if(selectNumbers.length==5){
			System.out.println("\n共"+sumAnswers+"期，中"+sumTargets+"期");
		}
	}

	private static void dataCollector() throws IOException {
		Document doc;
		if(TEST){
			File input = new File("D:/test.htm");
			doc = Jsoup.parse(input, "GBK", "");
		}else{
			String url = "http://caipiao.taobao.com/lottery/awardresult/lottery_dj.htm" ;
			Connection con = Jsoup.connect(url).userAgent("Chrome").timeout(10000);
			System.out.println("发送请求...\n");
			doc = con.get();
		}
		
		//打印
		//System.out.println(doc.html());
		
		//1:1 2:23 3:45 4:2 5:24 6:46 (x-1)%3*22+((x-1)/3+1)
		
		for(int n=0; n<65; n++){
			BT2.Answer initItem =  new BT2().new Answer(); 
			answerList.add(initItem);
		}
		Elements spans = doc.select("span[class=i-b3]");
		int i=1;
		for(Iterator<Element> it=spans.iterator();it.hasNext();){
			Element e = it.next();
			String number = e.text().indexOf("-")>=0?"":e.text();//text默认实现了类似trim的功能
			
			if(number.length()!=14){
				number = "";
			}
			
			int sn = (i-1)%3*22+((i-1)/3+1);
			i++;

			if(sn>=((TPA_SN==0)?66:TPA_SN)){
				if(!"".equals(number) && sn==TPA_SN && "".equals(TPAN)){
					TPAN = number;
				}
				continue;
			}

			BT2.Answer a = new BT2().new Answer(number,sn);
			if(!"".equals(number) && sn>snMax){
				snMax = sn;
			}
			answerList.set(sn-1,a);
		}
		
		latestAnswer = answerList.get(snMax-1);
		
		//打印
		for(BT2.Answer a : answerList){
			if(printFlag){
				System.out.println((a.sn<10?"0"+a.sn:a.sn)+"："+a.answer);
			}
		}
	}
	
	private static boolean analyzer(){
		
		//打印
		System.out.println("\n分析中...");
		for(BT2.Option o : optionList){
			for(BT2.Answer a : answerList){
				o.targetCheck(a);
			}
		}

//		Collections.sort(optionList,new BT2().new OptionComparator());
		
		if(snMax>numberRateScope){
			String numbers = "";
//			for(int n=snMax-1;n>snMax-1-numberRateScope;n--){
//				BatchTest.Answer a = answerList.get(n);
//				numbers +=a.answer+" "; 
//			}
			for(int n=snMax-numberRateScope;n<snMax;n++){
				BT2.Answer a = answerList.get(n);
				if("".equals(a.answer)){continue;}
				String numArray[] = a.answer.split(" ");
				for(int z=0;z<numArray.length;z++){
					BT2.SingleNumber btsn = numberMap.get(numArray[z]);
					btsn.targetTimes++;
					btsn.latestAppear = n+1;
					numberMap.put(numArray[z],btsn);
				}
				numbers +=a.answer+" "; 
			}
			numbers = numbers.trim();
//			String numArray[] = numbers.split(" ");
//			for(int z=0;z<numArray.length;z++){
//				numberMap.put(numArray[z],numberMap.get(numArray[z])+1);
//			}
			Set<?> set = numberMap.entrySet();
			@SuppressWarnings("unchecked")
			Map.Entry<String,BT2.SingleNumber>[] entries = (Map.Entry<String,BT2.SingleNumber>[]) set.toArray(new Map.Entry[set.size()]);
		    Arrays.sort(entries, new Comparator<Object>() {
		            public int compare(Object arg0, Object arg1) {
		            	@SuppressWarnings("unchecked")
						BT2.SingleNumber key1 = ((Map.Entry<String,BT2.SingleNumber>) arg0).getValue();
		    			@SuppressWarnings("unchecked")
						BT2.SingleNumber key2 = ((Map.Entry<String,BT2.SingleNumber>) arg1).getValue();
		                if(key1.targetTimes!=key2.targetTimes){
		                	return ((Integer)(key2.targetTimes)).compareTo((Integer)(key1.targetTimes));
		                }else if(key2.latestAppear!=key1.latestAppear){
		                	return ((Integer)(key2.latestAppear)).compareTo((Integer)(key1.latestAppear));
		                }else{
		                	return (key2.number).compareTo(key1.number);
		                }
		            }
	        });
		    
		    //打印
//		    if(printFlag){
		    	System.out.println("\n");
//		    }
		    
	    	List<BT2.SingleNumber> targetRate = new ArrayList<BT2.SingleNumber>();	
	    	Map<String,BT2.SingleNumber> snMap = new HashMap<String,BT2.SingleNumber>();
		    int ltt = 0; //last target time	
			int rank = 0;
			int sumMin = 0;
			int sumMax = 0;
		    for (Map.Entry<String,BT2.SingleNumber> e : entries) {
		    	  if(e.getValue().targetTimes!=ltt){
		    		  rank++;
		    		  e.getValue().rank = rank;
		    		  ltt = e.getValue().targetTimes;
		    	  }else{
		    		  e.getValue().rank = rank;
		    	  }
			      targetRate.add((BT2.SingleNumber)e.getValue());
			      snMap.put(e.getKey(),e.getValue());
			      //打印
//		    	  if(printFlag){
			      System.out.println("数字"+e.getKey()+"的频率为："+e.getValue().targetTimes+"次————第"+e.getValue().rank+"阶层");
//		    	  }
		     }

		    BT2.SingleNumber sn0 = targetRate.get(0);
		    BT2.SingleNumber sn1 = targetRate.get(1);
		    BT2.SingleNumber sn2 = targetRate.get(2);
		    BT2.SingleNumber sn3 = targetRate.get(3);
		    BT2.SingleNumber sn4 = targetRate.get(4);
		    BT2.SingleNumber sn5 = targetRate.get(5);
		    BT2.SingleNumber sn6 = targetRate.get(6);
		    BT2.SingleNumber sn7 = targetRate.get(7);
		    BT2.SingleNumber sn8 = targetRate.get(8);
		    BT2.SingleNumber sn9 = targetRate.get(9);
		    BT2.SingleNumber sn10 = targetRate.get(10);

		    sumMin = sn0.rank + sn1.rank + sn2.rank 	+ sn3.rank + sn4.rank + sn5.rank + sn6.rank + sn7.rank;
		    sumMax = sn8.rank + sn9.rank + sn10.rank 	+ sn3.rank + sn4.rank + sn5.rank + sn6.rank + sn7.rank;
		    
			for(BT2.Option o : optionList){
				o.setSumRank(snMap);
			}
			
			Collections.sort(optionList,new BT2().new RankComparator());

			//打印
			if(printFlag){
				System.out.println("\n第"+(snMax+1)+"期  分析结果：\n");
			}
		    
		    
		    //打印
		    if(printFlag){
		    	System.out.println("\n");
		    }

		    
		    for(BT2.Option o : optionList){
//		    	if(selectNumbers.length==5){
//			    	if(o.containTheNumber(sn0) && o.containTheNumber(sn1) && o.containTheNumber(sn2)){
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("高同含—— 错过"+o.option);
//			    		}
//			    		continue;
//			    	}
//			    	if(o.containTheNumber(sn3) && o.containTheNumber(sn4) && o.containTheNumber(sn5)){
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("中同含—— 错过"+o.option);
//			    		}
//			    		continue;
//			    	}
//			    	if(o.containTheNumber(sn6) && o.containTheNumber(sn7) && o.containTheNumber(sn8)){
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("低同含—— 错过"+o.option);
//			    		}
//			    		continue;
//			    	}
//			    	if(!o.containTheNumber(sn0) && !o.containTheNumber(sn1) && !o.containTheNumber(sn2)){
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("高不含—— 错过"+o.option);
//			    		}
//			    		continue;
//			    	}
//			    	if(!o.containTheNumber(sn6) && !o.containTheNumber(sn7) && !o.containTheNumber(sn8)){
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("低不含—— 错过"+o.option);
//			    		}
//			    		continue;
//			    	}
//		    	}
		    	
		    	if(selectNumbers.length==8){
//			    	if(o.targetTimes==0){
//	//		    	testPrint[2] = (Integer.parseInt(testPrint[2])+1)+"";
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("处女号—— 错过"+o.option);
//			    		}
//			    		continue;
//			    	}
//			    	if(o.targetTimes==1){
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("少女号—— 错过"+o.option);
//			    		}
//			    		continue;
//			    	}
//			    	if( o.containTheNumber(sn0) && o.containTheNumber(sn1) && o.containTheNumber(sn2) && o.containTheNumber(sn3) && o.containTheNumber(sn4)){
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("高同含—— 错过"+o.option);
//			    		}
//			    		continue;
//			    	}
//			    	if( o.containTheNumber(sn5) && o.containTheNumber(sn6) && o.containTheNumber(sn7) && o.containTheNumber(sn8) && o.containTheNumber(sn9)){
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("中同含—— 错过"+o.option);
//			    		}
//			    		continue;
//			    	}
//			    	if( o.containTheNumber(sn10)){
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("低同含—— 错过"+o.option);
//			    		}
//			    		continue;
//			    	}
//			    	if(!o.containTheNumber(sn0) && !o.containTheNumber(sn1) && !o.containTheNumber(sn2)){
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("高不含—— 错过"+o.option);
//			    		}
//			    		continue;
//			    	}
//			    	if( !o.containTheNumber(sn8) && !o.containTheNumber(sn9) && !o.containTheNumber(sn10)){
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("低不含—— 错过"+o.option);
//			    		}
//			    		continue;
//			    	}
//			    	if(o.containTheNumber(sn0) && o.containTheNumber(sn1) && o.containTheNumber(sn2) && o.containTheNumber(sn3) && o.containTheNumber(sn4) && o.containTheNumber(sn5)){
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("高六连—— 错过"+o.option);
//			    		}
//			    		continue;
//			    	}
//			    	if(o.containTheNumber(sn3) && o.containTheNumber(sn4) && o.containTheNumber(sn5) && o.containTheNumber(sn6) && o.containTheNumber(sn7) && o.containTheNumber(sn8)){
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("低六连—— 错过"+o.option);
//			    		}
//			    		continue;
//			    	}
//			    	if(o.containTheNumber(sn6) && o.containTheNumber(sn7) && o.containTheNumber(sn8) && o.containTheNumber(sn9) && o.containTheNumber(sn10)){
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("底五连—— 错过"+o.option);
//			    		}
//			    		continue;
//			    	}
			    	
//			    	if(o.isContinued){//去除4个号码
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("连续号—— 错过"+o.option);
//			    		}
//		    			continue;
//		    		}
			    	
//		    		if(!o.containTheNumber(sn0) && !o.containTheNumber(sn1)){
//		    			if(o.containTheNumber(sn9) && o.containTheNumber(sn10)){
//		    				
//		    			}else{
//		    				if(o.getNumbersOfSame(TPAN)==5){
//		    					//打印
//		    					System.out.println("无冠亚—— 错过"+o.option);
//		    				}
//		    				continue;
//		    			}
//		    		}
//		    		if(!o.containTheNumber(sn9) && !o.containTheNumber(sn10)){
//		    			if(o.containTheNumber(sn0) && o.containTheNumber(sn1)){
//		    				
//		    			}else{
//			    			if(o.getNumbersOfSame(TPAN)==5){
//				    			//打印
//				    			System.out.println("无摸底—— 错过"+o.option);
//				    		}
//			    			continue;
//		    			}
//		    		}
//			    	
//		    		if(o.getNumbersOfSame(latestAnswer.answer)==2){
//			    		if(o.getNumbersOfSame(TPAN)==5){
//			    			//打印
//			    			System.out.println("二二重—— 错过"+o.option);
//			    		}
//			    		continue;
//			    	}
//		    		
//			    	if(latestAnswer.containTheNumber(sn0) && latestAnswer.containTheNumber(sn1)){
//			    		if(o.containTheNumber(sn0) && o.containTheNumber(sn1) && o.containTheNumber(sn2) && o.containTheNumber(sn3)){
//			    			if(o.getNumbersOfSame(TPAN)==5){
//				    			//打印
//				    			System.out.println("冠亚四—— 错过"+o.option);
//				    		}
//			    			continue;
//			    		}
//			    	}
			    	
		    	}
		    	
//		    	if(o.containTheResult(snMax)){
//		    		continue;
//		    	}
		    	
//		    	int c = 0;
//		    	if(o.containTheNumber(sn0)||o.containTheNumber(sn1)){
//		    		c++;
//		    	}
//		    	if(o.containTheNumber(sn9)||o.containTheNumber(sn10)){
//		    		c++;
//		    	}
//		    	if(c==2){
//		    		advice.put(o.option,o);
//		    		adviceList.add(o);
//		    	}
//		    	if(o.containTheResult(snMax)){
//		    		if(o.getNumbersOfSame(TPAN)==5){
//		    			//打印
//		    			System.out.println("当前期—— 错过"+o.option);
//		    		}
//		    		continue;
//		    	}
		    	if(o.sumRank<((sumMin+sumMax)/2-3) || o.sumRank>((sumMin+sumMax)/2+3)){
		    		advice.put(o.option,o);
		    		adviceList.add(o);
//		    		if(o.getNumbersOfSame(TPAN)==5){
//			    		//打印
//			    		System.out.println("阶层过滤—— 错过"+o.option);
//		    		}
		    		continue;
		    	}
		    	if((o.containTheNumber(sn2)|| o.containTheNumber(sn1)) && o.containTheNumber(sn0) && (o.containTheNumber(sn5)||o.containTheNumber(sn6)) && (o.containTheNumber(sn9) && o.containTheNumber(sn10))){
//		    		advice.put(o.option,o);
//		    		adviceList.add(o);
		    	}//else 
//		    		if(!o.containTheNumber(sn0)&&!o.containTheNumber(sn1) && o.containTheNumber(sn2) && o.containTheNumber(sn5) && (o.containTheNumber(sn9) || o.containTheNumber(sn10))){
//		    		advice.put(o.option,o);
//		    		adviceList.add(o);
//		    	}else 
//		    		if(!o.containTheNumber(sn10)&&!o.containTheNumber(sn9) && o.containTheNumber(sn8) && o.containTheNumber(sn5) && (o.containTheNumber(sn0)||o.containTheNumber(sn1)) ){
//		    		advice.put(o.option,o);
//		    		adviceList.add(o);
//		    	}
		    }
		    
		}else{
			//打印
			System.out.println("设置有误！TPA_SN至少为（numberRateScope+2），即："+(numberRateScope+2));
			return false;
		}
		//打印
		System.out.println("\n分析结束。");
		return true;
	}
	
	private static void select(int head, int index, String[] n, String[] sn){
		int k = sn.length;
		for(int i=head; i<n.length+index-k; i++)   { 
			if(index < k){ 
				sn[index-1] = n[i]; 
				select(i+1,index+1,n,sn); 
			}else if(index == k){ 
				sn[index-1] = n[i]; 
				
				addToOptionList(sn);//产生一个数字组合sn
				
				select(i+1,index+1,n,sn); 
			}else{ 
				return; 
			} 
		}
	}
	
	
	private static void addToOptionList(String[] r){
		String result = ""; 
		for(int j=0;j<r.length;j++){
			result += r[j]+" ";
		}
		result = result.trim();
		optionList.add(new BT2().new Option(result,r.length,++counter));
	}
	
	public class OptionComparator implements Comparator<Object>{

	    public int compare(Object o1,Object o2) {
	    	BT2.Option op1=(BT2.Option)o1;
	    	BT2.Option op2=(BT2.Option)o2;  
	       if(op1.targetTimes<op2.targetTimes)
	           return 1;
	       else
	           return 0;
	       }

	}
	public class RankComparator implements Comparator<Object>{

	    public int compare(Object o1,Object o2) {
	    	BT2.Option op1=(BT2.Option)o1;
	    	BT2.Option op2=(BT2.Option)o2;  
	       if(op1.sumRank>op2.sumRank)
	           return 1;
	       else
	           return 0;
	       }

	}

}

//URL url=new URL("http://www.google.com"); 
//BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream())); 
//	String s=""; 
//	StringBuffer sb=new StringBuffer(""); 
//	while((s=br.readLine())!=null) 
//	{     
//   sb.append(s+"\r\n");    
//	} 
//	br.close(); 
//
//File input = new File("D:/webpage.html");
//if(!input.exists()){
//	input.createNewFile();
//}
//FileWriter fw=new FileWriter(input);                                    // 创建文件字符输出流
//BufferedWriter bw=new BufferedWriter(fw); 
//bw.write(sb.toString());
//Document doc = Jsoup.parse(input, "UTF-8", "www.google.com");
